'''
图片-----超人，元宝，财神，背景图
类-----超人，元宝，元宝掉落
操作-----超人左右移动，元宝向下随机降落
游戏判定-----元宝落地后失败
'''
import pygame
from pygame.locals import *
import random
import time
# ----------------------------------------------------------------------------
# ********************超人类********************
class Superman(object):
    coin_num = 0
    def __init__(self, screen_tmp):
        self.x = 500
        self.y =500
        self.screen = screen_tmp
        self.image = pygame.image.load('cr.png')
# ----------------------------------------------------------------------------
    # ********************超人移动********************
    def move_left(self):
        self.x -= 70
    def move_right(self):
        self.x += 70
# ----------------------------------------------------------------------------
    # ********************超人屏幕展示********************
    def display(self):
        self.screen.blit(self.image, (self.x, self.y))
#----------------------------------------------------------------------------
    # ********************元宝类********************
class  Coin(object):
    def __init__(self, screen_tmp):
        self.x = 0
        self.y = 0
        self.screen = screen_tmp
        self.direction='right'
        self.coin_list = []
# ----------------------------------------------------------------------------------
    def move(self):
        if self.direction == 'right':
            self.x += 3
        elif self.direction == 'left':
            self.x -= 3
        if self.x > 550:
            self.direction = 'left'
        elif self.x < 0:
            self.direction = 'right'
# ----------------------------------------------------------------------------------
    def display(self):
        self.move()
        for apple in self.coin_list:
            apple.display()
            apple.move()
# ----------------------------------------------------------------------------------
    # ********************元宝随机掉落********************
    def down(self):
        random_num=random.randint(1,200)
        if random_num==8 or random_num==20:
            self.coin_list.append(CoinDown(self.screen, self.x, self.y))
# ----------------------------------------------------------------------------------
# ********************元宝降落类********************
class CoinDown(object):
    def __init__(self, screen_tmp, x, y):
        self.x =x
        self.y =y
        self.image = pygame.image.load('yb.png')
        self.screen = screen_tmp
# ----------------------------------------------------------------------------------
    # ********************元宝屏幕展示********************
    def display(self):
        self.screen.blit(self.image, (self.x, self.y))
# ----------------------------------------------------------------------------------
    # ********************元宝降落********************
    def move(self):
        self.y+=0.6
# ---------------------------------------------------------------------------
    # ********************超人按键控制********************
def key_control(superman):
    for event in pygame.event.get():
        if event.type == QUIT:
            print("exit")
            exit()
        elif event.type == KEYDOWN:
            if event.key == K_LEFT:
                print('left')
                superman.move_left()
            elif event.key == K_RIGHT:
                print('right')
                superman.move_right()
            elif event.key == K_SPACE:
                print('space')
                superman.jump()
# ----------------------------------------------------------------------------
def main():
    #游戏初始化
    pygame.init()
    #设置屏幕大小
    screen = pygame.display.set_mode((1000, 700))
    #加载背景图片
    background = pygame.image.load('bj .png')
    cs=pygame.image.load('cs.png')
    superman = Superman(screen)
    coin = Coin(screen)
    while True:
        #绘制背景图
        screen.blit(background, (0, 0))
        #设置分数字体
        score_font = pygame.font.SysFont("MS Gothic", 80)
        # 更新分数
        score_surface = score_font.render(str(Superman.coin_num), True, (0, 0, 255))
        # 绘制财神
        screen.blit(cs, (600, 6))
        #绘制分数
        screen.blit(score_surface, (880, 100))
        superman.display()
        coin.down()
        coin.display()
        #当元宝落到超人面前接到元宝，元宝消失
        for coins in coin.coin_list:
            if superman.x <= coins.x <= superman.x + 120 and coins.y >= superman.y -60:
                s1 = pygame.image.load('1.png')
                screen.blit(s1, (50, 150))
                s1 = pygame.image.load('1.png')
                screen.blit(s1, (350, 440))
                s1 = pygame.image.load('1.png')
                screen.blit(s1, (600, 280))
                s1 = pygame.image.load('1.png')
                screen.blit(s1, (800, 530))
                # 移除元宝列表
                coin.coin_list.remove(coins)
                #元宝接近地面游戏结束
            elif coins.y >= 750:
                #font.render参数意义：.render（内容，是否抗锯齿，字体颜色，字体背景颜色）
                #抗锯齿，也叫反锯齿，它的作用是边缘柔化，可以消除混叠等。
                # 因为在游戏中，受到一些分辨的制约，会导致一些物体的边缘多少会呈现一些三角形状的锯齿，
                # 而抗锯齿这个选项就是对游戏内3D图像边缘实施柔化处理，开启抗锯齿就会显得平滑一些，最终呈现的效果都比较接近与实体。
                # 所以，这也是提高画质的一种途径。
                over = score_font.render('Game Over', True, (0, 0, 255))
                coin.screen.blit(over, (250,200))
                #分数更新
                pygame.display.update()
                time.sleep(2)
                #退出游戏
                pygame.quit()
        #界面图像更新
        pygame.display.update()
        #按键控制
        key_control(superman)


# ----------------------------------------------------------------------------
if __name__ == '__main__':
    main()
